package com.dcbeida.activity.changepassword;


import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.dcbeida.R;
import com.dcbeida.bean.Result_Api;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.model.ChangePsModel;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by hp on 2016/5/12.
 */
public class ChangePsPresenter implements ChangePsContract.Present {


    private ChangePsContract.View view;

    private ChangePsModel model;

    private Activity act;

    public ChangePsPresenter(ChangePsContract.View view, Activity act) {
        //我这里直接把activity作为view，所以不需要
        //mView.setPresenter(this);
        this.view = view;
        model = ChangePsModel.newInstance();
        this.act=act;

    }

//    @Override
//    public void login() {
//        if (validator()) {
//
//            //model.getDate(view.getAccount(),view.getPassword(),new LgoinCallBack(act,User.class,lis));
//        }
//    }

    @Override
    public void start() {

    }

    /**
     * 登录参数校验
     *
     * @return
     */
    private boolean validator() {
        if (TextUtils.isEmpty(view.getOldPassword())
                ||view.getOldPassword().trim().length()<6||view.getOldPassword().trim().length()>16) {
            view.showATip(R.string.qzqtxjmm);
            return false;
        }

        if (TextUtils.isEmpty(view.getNewPassword())
                ||view.getNewPassword().trim().length()<6
                ||view.getNewPassword().trim().length()>12) {
            view.showATip(R.string.qzqtxxmm);
            return false;
        }

        if (!view.getNewPassword().equals(view.getModfiyNewPassword())) {
            view.showATip(R.string.lcsrmmbyz);
            return false;
        }

        return true;
    }

    //取消dialog 取消请求
    DialogInterface.OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model.getCancel();
        }
    };

    @Override
    public void change() {
        if (validator()) {
            model.getDate(view.getOldPassword(),view.getModfiyNewPassword(),new ChangePsCallBack(act,Result_Api.class,lis));
        }
    }


    final class ChangePsCallBack<T> extends DialogCallback<T> {

        public ChangePsCallBack(Activity activity, Class<T>tclass, DialogInterface.OnCancelListener listener) {
            super(activity, tclass, listener);
        }

        @Override
        public void onResponse(T data) {
            if(data!=null) {
                if(data instanceof Result_Api){
                    if(((Result_Api) data).getCode()==1){
                        view.showATip(((Result_Api) data).getMsg());
                        view.changePsSuccess();
                    }
                }
            }

        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showATip("网络加载有问题，请重试");

        }
    }



}
